package com.t.bricks.bussiness.db.dao.userandright.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.t.bricks.bussiness.db.dao.userandright.UserAndRightDao;
import com.t.bricks.bussiness.db.entity.userandright.SimpleRight;
import com.t.bricks.bussiness.db.entity.userandright.UserAndRightSimpl;
import com.t.bricks.bussiness.db.entity.userandright.UserRightInfo;
import com.t.bricks.bussiness.db.mapper.userandright.UserAndRightMapper;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;

@Repository
public class UserAndRightDaoImpl implements UserAndRightDao {
	
	@Autowired
	private UserAndRightMapper userAndRightMapper;

	/**
	 * 初始化查询权限信息集合
	 * @param sUserId
	 * @return
	 */
	@Override
	public List<SimpleRight> findRightListByInit(String sUserId) {
		List<SimpleRight> list = null;
		try {
			list = userAndRightMapper.findRightListByInit(sUserId);
		} catch (Exception e) {
			Log.error("查询权限信息数据发生异常:", e.getMessage());
		}
		
		if (null == list) {
			list = new ArrayList<SimpleRight>();
		}
		
		return list;
	}
	
	/**
	 * 添加权限信息
	 * @param sUserId
	 * @param sRightList
	 * @param sCreator
	 * @return
	 */
	@Transactional
	@Override
	public MsgEmity add(String sUserId, String sRightList, String sCreator) {
		String[] str = sRightList.split(",");
		List<String> list = java.util.Arrays.asList(str);// 权限编号集合转换
		
		int iCode = 7003;
		try {
			int j = userAndRightMapper.findCountByUserId(sUserId);
			if (j > 0 ) {
				int k = userAndRightMapper.del(sUserId);
				if (k < 1) {
					iCode = 7001;
					Log.error("删除未能得到预期影响值(预期大于等于1):", k);
					throw new RuntimeException("删除数据失败！");
				}
			}
			
			if (null != list && list.size() > 0 && !list.contains("")) {
				int i = userAndRightMapper.add(sUserId, list, sCreator);
				if (i < 1) {
					iCode = 7002;
					Log.error("保存未能得到预期影响值(预期大于等于1):", i);
					throw new RuntimeException("保存数据失败！");
				}
			}
			
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			Log.error("保存时发生异常:", e.getMessage());
			return new MsgEmity(false, "保存失败！", iCode);
		}
		
		return new MsgEmity(true, "保存成功！", 7999);
	}

	/**
	 * 删除权限信息
	 * @param sUserId
	 * @return
	 */
	@Override
	public MsgEmity del(String sUserId) {
		try {
			int i = userAndRightMapper.findCountByUserId(sUserId);
			if (i > 0 ) {
				userAndRightMapper.del(sUserId);
			}
		} catch (Exception e) {
			Log.error("删除时发生异常:", e.getMessage());
			return new MsgEmity(false, "删除失败！", 92);
		}
		
		try {
			int i = userAndRightMapper.findCountByUserId(sUserId);
			if (i > 0) {
				 return new MsgEmity(false, "删除失败！", 91);
			}
		} catch (Exception e) {
			Log.error("查询时发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 93);
		}
	
		return new MsgEmity(true, "删除成功！", 99);
	}
	
	/**
	 * 根据用户编号获取与权限关联的信息
	 * @param sUserId
	 * @return
	 */
	@Override
	public MsgEmity findByUserId(String sUserId) {
		List<UserAndRightSimpl> list = null;
		try {
			list = userAndRightMapper.findByUserId(sUserId);
			if (null == list) {
				return new MsgEmity(false, "指定编号的用户不存在！", 7001);
			}
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 7002);
		}
		
		return new MsgEmity(true, "查询成功！", list);
	}
	
	/**
	 * 根据用户编号查询用户权限
	 * @param sLoginUserId
	 * @return
	 */
	@Override
	public MsgEmity findRightInfoByUser(String sLoginUserId) {
		List<UserRightInfo> list = null;
		try {
			list = userAndRightMapper.findRightInfoByUser(sLoginUserId);
			if (null == list) {
				Log.error("查询数据发生异常,执行的返回值为:null,预期值不应为null");
				return new MsgEmity(false, "查询返回值与预定值不同！", 7001);
			}
		} catch (Exception e) {
			Log.error("根据用户编号查询用户权限发生异常:", e.getMessage());
			return new MsgEmity(false, "查询发生异常！", 7002);
		}

		return new MsgEmity(true, "查询成功！", list);
	}

	/**
	 * 根据用户查询用户所拥有的权限编号集合
	 * @param sUserId
	 * @return
	 */
	@Override
	public MsgEmity findRightId(String sUserId) {
		List<String> list = null;
		try {
			list = userAndRightMapper.findRightId(sUserId);//根据用户查询用户所拥有的权限所在路径值
		} catch (Exception e) {
			Log.error("查询用户所拥有的权限数据发生异常:", e.getMessage());
			return MsgEmity.err(7001, "查询失败");
		}
		
		return MsgEmity.success(list, "查询成功");
	}
	
	/**
	 * 获取所有权限信息
	 * @return
	 */
	@Override
	public MsgEmity findRightAll() {
		List<SimpleRight> list = null;
		try {
			list = userAndRightMapper.findRightAll();//获取所有权限信息
		} catch (Exception e) {
			Log.error("查询所有权限信息数据发生异常:", e.getMessage());
			return MsgEmity.err(7001, "查询失败");
		}
		
		return MsgEmity.success(list, "查询成功");
	}
	
}
